//
//  CategoryCollectionViewController.m
//  悠狸
//
//  Created by Grant on 15/8/31.
//  Copyright (c) 2015年 Dung. All rights reserved.
//

#import "CategoryCollectionViewController.h"
#import "SecondCategories.h"
#import "SingleCell.h"
#import "Asyn.h"
#import "Tools.h"
#import "URL.h"
#import "SingleDeftailViewController.h"
#import "SingleModel.h"



@interface UIViewController () <UICollectionViewDataSource, UICollectionViewDelegate>


@end

@implementation CategoryCollectionViewController


- (void)dealloc
{
    [_collection release];
    [_array release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self create];
    
    self.navigationController.navigationBar.translucent = NO;
    [self parser];
}

- (void)create
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(160*OffWidth, 240*OffHeight);
    //    layout.minimumLineSpacing = 10;
    //    layout.minimumInteritemSpacing = 0;
    
    layout.sectionInset = UIEdgeInsetsMake(15*OffWidth, 15*OffHeight, 15*OffWidth,15*OffHeight);
    self.collection =[[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64- 49) collectionViewLayout:layout] autorelease];
    _collection.delegate = self;
    _collection.dataSource =self;
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    
    [_collection registerClass:[SingleCell class] forCellWithReuseIdentifier:@"single"];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    SingleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"single" forIndexPath:indexPath];
    
    //cell.backgroundColor = [UIColor greenColor];
    
    SingleModel *model = [_array objectAtIndex:indexPath.item];
    [cell setsingleModeL:model];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    SingleDeftailViewController *sin = [[[SingleDeftailViewController alloc] init] autorelease];
    
    SingleModel *model = [_array objectAtIndex:indexPath.row];
    sin.model = model;
    
    [self.navigationController pushViewController:sin animated:YES];
    
}

#define SerVer @"http://api.liwushuo.com/v1/item_subcategories/7/items?limit=20&offset=0";
- (void)parser
{

    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v1/item_subcategories/%@/items?limit=20&offset=0", self.secondCategory.myId];
    
    
    [Asyn  startParser:str partemer:nil file:@"data70" block:^(NSData *data) {
        
        //存储
        [Tools toolsOfSaveLibraryPathName:@"data70" data:data];
        // 解析
        NSDictionary *dic = [Tools toolsOfJsonDataType:dicType fileName:@"data70"];
        
        
        NSDictionary *dic1 =[dic objectForKey:@"data"];
        
        NSArray *arr= [dic1 objectForKey:@"items"];
        
        for (NSDictionary *dic3 in arr) {
            
            SingleModel *single =[[SingleModel alloc] init];
            
            [single setValuesForKeysWithDictionary:dic3];
            
            [_array addObject:single];
        }
#pragma 特记刷新位置
        [_collection reloadData];
        
    }];
    
}
@end
